#pragma once

#include <iostream>
#include <fstream>
#include <string>
#include <vector>


struct PurchaseRedemptionEntry
{
    enum CashFlag
    {
        DENY = 0,
        ALLOW = 1,
        MUST = 2,
        REFUND = 3, // 退补

        UNKNOWN,
    };
    int code;
    std::string name;
    int quantity;
    CashFlag cash_flag;
    int margin_rate;       // 申购现金替代保证金率, N5(2) 最后两位是小数
    int purchase_amount;   // 申购替代金额， N10(4)
    int redemption_amount; // 赎回替代金额， N10(4)
};

class PurchaseRedemptionList
{
private:
    int s2iWithComma(std::string &s)
    {
        int ret = 0;
        for (auto c : s)
        {
            if (c != ',')
            {
                ret = ret * 10 + c - '0';
            }
        }
        return ret;
    }

public:
    PurchaseRedemptionList() {}
    ~PurchaseRedemptionList() {}
    std::vector<PurchaseRedemptionEntry> read(std::string filename)
    {
        std::ifstream file("/home/wang/Documents/github/internship/mddp/申购赎回清单.txt"); // 替换为你的txt文档路径

        if (!file)
        {
            // cout << "无法打开文件" << endl;
            return {};
        }

        std::string line;
        for (int i = 0; i < 44; ++i)
            std::getline(file, line); // 读取表头

        std::vector<PurchaseRedemptionEntry> ret;
        while (std::getline(file, line))
        {
            if (line[0] != '-')
            {
                std::string code, name, quantity, cashFlag, margin_rate, purchaseAmount, redemptionAmount;

                auto pos = line.find_first_not_of(" ", 0);
                auto pos2 = line.find_first_of(" ", pos);
                code = line.substr(pos, pos2 - pos);

                auto split = [&]()
                {
                    pos = line.find_first_not_of(" ", pos2);
                    pos2 = line.find_first_of(" ", pos);
                    return line.substr(pos, pos2 - pos);
                };

                name = split();
                quantity = split();
                cashFlag = split();
                margin_rate = split();
                purchaseAmount = split();
                redemptionAmount = split();

                PurchaseRedemptionEntry entry;
                entry.code = stoi(code);
                entry.name = name;
                entry.quantity = s2iWithComma(quantity);
                if (cashFlag == "禁止")
                    entry.cash_flag = PurchaseRedemptionEntry::DENY;
                else if (cashFlag == "允许")
                    entry.cash_flag = PurchaseRedemptionEntry::ALLOW;
                else if (cashFlag == "必须")
                    entry.cash_flag = PurchaseRedemptionEntry::MUST;
                else if (cashFlag == "退补")
                    entry.cash_flag = PurchaseRedemptionEntry::REFUND;
                else
                    std::cout << "error" << std::endl;

                // margin_rate 删除最后一个 %
                margin_rate.pop_back();
                entry.margin_rate = stod(margin_rate) * 100;
                entry.purchase_amount = stod(purchaseAmount) * 10000;
                entry.redemption_amount = stod(redemptionAmount) * 10000;

                ret.emplace_back(std::move(entry));
            }
        }

        file.close();

        return ret;
    }
};

